/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentacion;

import com.alee.laf.WebLookAndFeel;
import dao.OrderDao;
import java.awt.event.ActionEvent;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import modelos.DetalleOrden;
import modelos.Orden;
import org.jdesktop.observablecollections.ObservableCollections;
import org.jdesktop.observablecollections.ObservableList;
import recursos.XML;

/**
 *
 * @author luramirez
 */
public class Ordenes extends javax.swing.JFrame {

    /**
     * Creates new form Ordenes
     */
    private OrderDao orderDao;
    private ObservableList<Orden> ordenes;
    private GuardarAction guardar = new GuardarAction();
    private NuevoAction nuevo = new NuevoAction();
    private EliminarAction eliminar = new EliminarAction();
    private CancelarAction cancelar = new CancelarAction();
    //
    private NuevoDetAction nuevoDet = new NuevoDetAction();
    private EliminarDetAction eliminarDet = new EliminarDetAction();

    public Ordenes() {

        orderDao = new OrderDao();
        try {
            ordenes = ObservableCollections.observableList(new LinkedList(orderDao.getOrdenes()));
        } catch (Exception ex) {
            Logger.getLogger(Ordenes.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
        init();
    }

    private void init() {
        btnNuevo.setAction(nuevo);
        btnEliminar.setAction(eliminar);
        btnGuardar.setAction(guardar);
        btnNuevoDet.setAction(nuevoDet);
        //btnEliminarDet.setAction(eliminarDet);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        btnEliminar = new com.alee.laf.button.WebButton();
        btnNuevo = new com.alee.laf.button.WebButton();
        btnGuardar = new com.alee.laf.button.WebButton();
        btnNuevoDet = new com.alee.laf.button.WebButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblOrdenes = new com.alee.laf.table.WebTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        tblDetOrdenes = new com.alee.laf.table.WebTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ordenes");

        btnEliminar.setText("webButton1");

        btnNuevo.setText("webButton1");

        btnGuardar.setText("webButton1");

        btnNuevoDet.setText("webButton4");

        tblOrdenes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${ordenes}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, tblOrdenes);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idOrden}"));
        columnBinding.setColumnName("Id Orden");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${fechaOrden}"));
        columnBinding.setColumnName("Fecha Orden");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idCliente}"));
        columnBinding.setColumnName("Id Cliente");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${dirCliente}"));
        columnBinding.setColumnName("Dir Cliente");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nomCliente}"));
        columnBinding.setColumnName("Nom Cliente");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane2.setViewportView(tblOrdenes);

        eLProperty = org.jdesktop.beansbinding.ELProperty.create("${selectedElement.detalleOrden}");
        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tblOrdenes, eLProperty, tblDetOrdenes);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idOrden}"));
        columnBinding.setColumnName("Orden");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${idProducto}"));
        columnBinding.setColumnName("Id Producto");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${descProducto}"));
        columnBinding.setColumnName("Desc Producto");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${precio}"));
        columnBinding.setColumnName("Precio");
        columnBinding.setColumnClass(Double.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cantidad}"));
        columnBinding.setColumnName("Cantidad");
        columnBinding.setColumnClass(Integer.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane4.setViewportView(tblDetOrdenes);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(10, 10, 10)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(btnNuevo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(btnEliminar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnGuardar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(btnNuevoDet, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(29, 29, 29))
                    .add(jScrollPane2))
                .add(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(10, 10, 10)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 148, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(10, 10, 10)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnNuevo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnEliminar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnGuardar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(10, 10, 10)
                .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 322, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnNuevoDet, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private class CancelarAction extends AbstractAction {

        public CancelarAction() {
            putValue(NAME, "Cancelar");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                ordenes.clear();
                ordenes = ObservableCollections.observableList(new LinkedList(orderDao.getOrdenes()));
            } catch (Exception ex) {
                Logger.getLogger(Ordenes.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private class NuevoAction extends AbstractAction {

        public NuevoAction() {
            putValue(NAME, "Nuevo");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            ordenes.add(new Orden());
            int i = ordenes.size() - 1;
            tblOrdenes.setRowSelectionInterval(i, i);
        }
    }

    private class EliminarAction extends AbstractAction {

        public EliminarAction() {
            putValue(NAME, "Eliminar");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int selected = tblOrdenes.getSelectedRow();
            Orden orden = ordenes.get(tblOrdenes.convertRowIndexToModel(selected));
            try {
                orderDao.procesarOrden(orden, 2);
                ordenes.remove(orden);
                JOptionPane.showMessageDialog(Ordenes.this,"Elemento Eliminado Correctamente");
            } catch (Exception ex) {
                Logger.getLogger(Ordenes.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private class GuardarAction extends AbstractAction {

        public GuardarAction() {
            putValue(NAME, "Guardar");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int selected = tblOrdenes.getSelectedRow();
            Orden orden = ordenes.get(tblOrdenes.convertRowIndexToModel(selected));
            try {
                orderDao.procesarOrden(orden, 1);
                
            } catch (Exception ex) {
                Logger.getLogger(Ordenes.class.getName()).log(Level.SEVERE, null, ex);
                
            }
        }
    }

    private class NuevoDetAction extends AbstractAction {

        public NuevoDetAction() {
            putValue(NAME, "Nuevo Detalle");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int selected = tblOrdenes.getSelectedRow();
            Orden orden = ordenes.get(tblOrdenes.convertRowIndexToModel(selected));

            orden.getDetalleOrden().add(new DetalleOrden());
            int i = orden.getDetalleOrden().size() - 1;
            tblDetOrdenes.setRowSelectionInterval(i, i);
        }
    }

    private class EliminarDetAction extends AbstractAction {

        public EliminarDetAction() {
            putValue(NAME, "Eliminar Detalle");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            int selected = tblDetOrdenes.getSelectedRow();
            int orderSelected = tblOrdenes.getSelectedRow();
            Orden orden = ordenes.get(tblOrdenes.convertRowIndexToModel(orderSelected));
            DetalleOrden detOrden = orden.getDetalleOrden().get(tblDetOrdenes.convertRowIndexToModel(selected));
            orden.getDetalleOrden().remove(detOrden);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            UIManager.setLookAndFeel(WebLookAndFeel.class.getCanonicalName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Ordenes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Ordenes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Ordenes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Ordenes.class.getName()).log(Level.SEVERE, null, ex);
        }
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Ordenes().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.alee.laf.button.WebButton btnEliminar;
    private com.alee.laf.button.WebButton btnGuardar;
    private com.alee.laf.button.WebButton btnNuevo;
    private com.alee.laf.button.WebButton btnNuevoDet;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private com.alee.laf.table.WebTable tblDetOrdenes;
    private com.alee.laf.table.WebTable tblOrdenes;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the ordenes
     */
    public ObservableList<Orden> getOrdenes() {
        return ordenes;
    }

    /**
     * @param ordenes the ordenes to set
     */
    public void setOrdenes(ObservableList<Orden> ordenes) {
        this.ordenes = ordenes;
    }
}
